<template>
    <ModalFormTemplate
      ref="modalRef"
      :open="open"
      :formConfig="formConfig"
      :title="title"
      :width="width"
      @cancel="onCancel"
      @finish="onFinish"
    />
</template>
<script setup lang="ts">
  import { computed, ref } from 'vue';
  import ModalFormTemplate from '@/components/template/form/modal-form-template.vue';

  const title ="OEM添加";//弹窗标题
  const width = 550;//弹窗宽度
  const modalRef = ref();//通用模态弹窗组件引用，用与调用模态弹窗组件内部方法
  const open = ref(false);//控制弹窗显示、隐藏
  const emits = defineEmits<{ dataReload: []; }>();//通知调用组件(父组件) 执行 父组件 的 @dataReload 的方法

  defineExpose({ show });//暴露接口 show(显示弹窗) 方法，供 父组件 调用

  /* [关闭弹窗] 1. 重置表单 2. 关闭弹窗 */
  function onCancel() {
    modalRef.value.resetForm();
    open.value = false;
  } 

  /* [显示弹窗] 1. 显示弹窗  */
  function show() {
    open.value = true;
  }

  /* [提交弹窗] 1. 调用子组件 提交方法 2. 提交成功后，关闭弹窗 3. 刷新父组件数据 */
  async function onFinish() {
    const params = modalRef.value?.getFormParams();
    const mergedParams = {...addParams,...params};//合并提交参数
    try {
      const ret = await modalRef.value?.postFormData(addAction, mergedParams);
      if (ret === true) {
        onCancel();
        emits('dataReload');
      }
    } catch (error) {}
  }
  const addAction = 'base.agent.oem.ServiceImpl.save';//save接口 方法名
  const addParams = {oem_id: 0};//save接口 参数

  /* [表单配置] */
  const formConfig = computed(() => [
    {
      title: 'OEM名称',
      dataIndex: 'oem_name',
      type: 'input' as const,
      maxlength: 250,
      required: true,
      defaultValue: ''
    },
    {
      title: 'OEM简称',
      dataIndex: 'oem_short_name',
      type: 'input' as const,
      maxlength: 50,
      required: true,
      defaultValue: ''
    },
    {
      title: '联系人',
      dataIndex: 'oem_contact_name',
      type: 'input' as const,
      maxlength: 200,
      required: true,
      defaultValue: ''
    },
    {
      title: '联系方式',
      dataIndex: 'oem_contact_phone',
      type: 'input-number' as const,
      maxlength: 13,
      required: true,
      defaultValue: ''
    },
    {
      title: 'OEM联系地址',
      dataIndex: 'oem_contact_address',
      type: 'input' as const,
      maxlength: 250,
      required: true,
      defaultValue: ''
    },
    {
      title: '状态', 
      dataIndex: 'oem_status',
      type: 'switch' as const,
      props: {
        checkedChildren: '启用',
        checkedValue: 1,
        unCheckedChildren: '停用',
        unCheckedValue: 2
      },
      defaultValue: 1
    },
    {
      title: '备注',
      dataIndex: 'remark',
      type: 'input' as const,
      maxlength: 250,
      required: false,
      defaultValue: ''
    }
  ]);
</script>
<style lang="less" scoped></style>
